using System.Web.Mvc;
using Complaint.Models;
using Complaint.Models.Dtos;

namespace Complaint.Areas.Admin.Controllers
{
    public class PasswordQuestionController : AdminBaseController
    {
        //
        // GET: /PasswordQuestion/

        public ActionResult Index()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();
                return View(model.Retrieve());
            }
        }

        public ActionResult Add()
        {
            var passwordQuestion = new PasswordQuestion();
            return PartialView("QuestionForm", passwordQuestion);
        }

        public ActionResult Edit(int id)
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new PasswordQuestionModel();
                var question = model.FindById(id);
                return PartialView("QuestionForm", question);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(PasswordQuestion passwordQuestion)
        {
            try
            {
                using (new UnitOfWorkScope(true))
                {
                    var model = new PasswordQuestionModel();
                    var question = model.FindById(passwordQuestion.PasswordQuestionID);
                    question.Question = passwordQuestion.Question;
                    model.Update(question);

                    return PartialView("ListControl", model.Retrieve());
                }
            }
            catch
            {
                return Content("Failed");
            }
        }

        [HttpPost]
        public ActionResult Add(PasswordQuestion passwordQuestion)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (new UnitOfWorkScope(true))
                    {
                        var model = new PasswordQuestionModel();
                        model.Create(passwordQuestion);

                        return PartialView("ListControl", model.Retrieve());
                    }
                }
                catch
                {
                    return Content("Failed");
                }
            }

            return PartialView("ListControl", new PasswordQuestion());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(short id)
        {
            try
            {
                using (new UnitOfWorkScope(true))
                {
                    var model = new PasswordQuestionModel();

                    model.Delete(model.FindById(id));
                    return PartialView("ListControl", model.Retrieve());
                }
            }
            catch
            {
                // error handling here
                return Content("Failed to delete due to an error.");
            }
        }
    }
}